/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.system.install.help;

import java.util.HashSet;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.ksplatform.modules.system.auth.menumanager.model.PlatMenu;
import cn.com.ksplatform.modules.system.auth.menumanager.service.PlatMenuService;
import cn.com.ksplatform.modules.system.auth.orgmanager.model.PlatOrg;
import cn.com.ksplatform.modules.system.auth.orgmanager.model.PlatStation;
import cn.com.ksplatform.modules.system.auth.orgmanager.service.PlatOrgService;
import cn.com.ksplatform.modules.system.auth.orgmanager.service.PlatStationService;
import cn.com.ksplatform.modules.system.auth.rolemanager.model.PlatRole;
import cn.com.ksplatform.modules.system.auth.rolemanager.service.PlatRoleService;
import cn.com.ksplatform.modules.system.auth.usermanager.model.PlatUser;
import cn.com.ksplatform.modules.system.auth.usermanager.service.PlatUserService;

/**
 * 系统初始化数据
 * 
 * @author X
 * 
 */
@Component("system.modules.install.DataInstallHelp")
public class DataInstallHelp {

	@Autowired
	private PlatUserService userService;

	@Autowired
	private PlatMenuService menuService;

	@Autowired
	private PlatRoleService roleService;
	
	@Autowired
	private PlatOrgService orgService;
	
	@Autowired
	private PlatStationService staionService;

	@Transactional
	public void installBaseData() {
		Set menus_set = new HashSet();
		Set role_set = new HashSet();
		Set station_set = new HashSet();
		
		// 角色
		PlatRole admin_role = new PlatRole();
		admin_role.setRoleName("系统管理员");
		admin_role.setRoleCode("ADMIN");
		admin_role.setEnabled("Y");
		roleService.save(admin_role);
		role_set.add(admin_role);
		
		//组织机构
		PlatOrg sgcc_org = new PlatOrg();
		sgcc_org.setOrgCode("SGCC");
		sgcc_org.setOrgName("国家电网公司");
		sgcc_org.setEnabled("Y");
		sgcc_org = orgService.save(sgcc_org);
		
		PlatStation station = new PlatStation();
		station.setStaName("营销部");
		station.setOrg(sgcc_org);
		station = staionService.save(station);
		station_set.add(station);
		
		// 用户数据
		PlatUser user_admin = new PlatUser();
		user_admin.setUserName("admin");
		user_admin.setPassWord("admin");
		user_admin.setRoles(role_set);
		user_admin.setStations(station_set);
		user_admin = userService.save(user_admin);
		
		
		// 菜单
		String[] menus = new String[] { "用户管理", "角色管理", "资源菜单管理", "组织机构管理",
				"系统字典管理", "系统参数管理", "权限设置", "系统监控" ,"图标集合"};
		String[] links = new String[] { "/view/modules/system/user/user.vm",
				"/view/modules/system/role/role.vm",
				"/view/modules/system/menu/menu.vm",
				"/view/modules/system/org/org.vm",
				"/view/modules/system/dict/dict.vm",
				"/view/modules/system/param/param.vm",
				"/view/modules/system/permit/permit.vm", "/druid/" ,"/ui/resource/plug/awesome/index.html"};
		String[] icons = new String[] { "fa fa-users", "fa fa-star",
				"fa fa-bookmark", "fa fa-institution", "fa fa-tasks",
				"fa fa-wrench", "fa fa-sliders", "fa fa-indent" ,"fa fa-meh-o" };
		PlatMenu menu = new PlatMenu(null, "系统管理", null,"fa fa-cog","Y", 0);
		Set<PlatMenu> children = new HashSet<PlatMenu>();
		menu.setChildren(children);
		for (int i = 0; i < menus.length; i++) {
			PlatMenu cmenu = new PlatMenu(null, menus[i], links[i],icons[i], "Y", i);
			cmenu.setParent_menu(menu);
			children.add(cmenu);
		}
		menuService.save(menu);
		
	}
}
